
import { _decorator, Component } from 'cc';
import { CursorController } from './CursorController';
import { SuperButton } from '../keyboard/base/SuperButton';
// import { MultiController } from '../../../global/MultiController';

const { ccclass, property } = _decorator;

@ccclass('CursorView')
export class CursorView extends Component {
    private _focusedSuperButton: SuperButton = null;

    start() {
        CursorController.instance().cursorView = this;
    }

    update(deltaTime: number) {
        this.updateRenderer();
    }

    updateRenderer() {
        if (this._focusedSuperButton) {
            this.node.worldPosition = this._focusedSuperButton.getCursorWorldPosition();
            this.node.active = this._focusedSuperButton.node.activeInHierarchy && this._focusedSuperButton.cursor.cursorVisible;
        }
    }

    onDestroy() {
        CursorController.instance().cursorView = null;
    }

    getFocusedSuperButton(): SuperButton {
        return this._focusedSuperButton;
    }

    focusItem(itemView: SuperButton) {
        if (!itemView) {
            // this.node.parent = null;
            CursorController.instance().hideCursor();
            this._focusedSuperButton = null;
            // Tooltip.hideUIBlockInput();
            return;
        }
        if (this._focusedSuperButton == itemView) {
            return;
        }

        this.node.worldPosition = itemView.getCursorWorldPosition();
        this.node.angle = itemView.cursor.cursorDir * 90;
        this._focusedSuperButton = itemView;
        this.updateRenderer();
    }

    public onFocusEvent(itemView: SuperButton) {
        // MultiController.getInstance().blockInput = false;
        this.focusItem(itemView);
    }

    public onBlurEvent(itemView: SuperButton) {
        // MultiController.getInstance().blockInput = false;
        if (this.getFocusedSuperButton() == itemView) {
            this.focusItem(null);
        }
    }
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.3/manual/zh/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.3/manual/zh/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.3/manual/zh/scripting/life-cycle-callbacks.html
 */
